<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hdholidayreadingquanitymodel extends CI_Model {

    protected $_CI;

    public static $searchField = array(
        'reading_quanity_time_create_start' => 'reading_quanity_time_create >=',
        'reading_quanity_time_create_end' => 'reading_quanity_time_create >=',
        'user_id' => 'user_id',

    );
    public static $likeField = array(
        'user_name' => 'user_name',
        'user_code' => 'user_code',
    );

    public static $sortField = array(
        'quanity' => 'quanity',
        'rank' => 'rank',
    );

    public function __construct(){
        parent::__construct();
        $this->_CI = & get_instance();
    }
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
    public function getReadingQuanityListSection($assist) {
        $this->db->from('hd_holiday_reading_quanity');
        $this->db->join('user','hd_holiday_reading_quanity.user_id = user.user_id','left');
        if(!empty($assist['where'])) $this->db->where($assist['where']);
        if(!empty($assist['like'])) $this->db->like($assist['like']);
        $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

        if(!empty($assist['sort']) && !empty($assist['sort'])) $this->db->order_by($assist['sort']['key'],$assist['sort']['dir']);
        $this->db->order_by('rank','asc');
        if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        return array($list,$count);
    }

    public function getReadingQuanityByUserId($user_id){
        $this->db->from('hd_holiday_reading_quanity');
        $this->db->where('$user_id',$user_id);
        $this->db->limit(1);
        $query = $this->db->get();
        $res = $query->row_array();

        return $res;
    }

/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
    public function createReadingQuanity($info){
        $this->db->insert('hd_holiday_reading_quanity',$info);
        $id = $this->db->insert_id();
        return $id;
    }


/*
| -------------------------------------------------------------------
|  UPDATE Functions
| -------------------------------------------------------------------
*/
    public function updateReadingQuanity($info){
        $this->db->from('hd_holiday_reading_quanity');
        $this->db->where('user_id',$info['user_id']);
        $this->db->limit(1);
        $query = $this->db->get();
        $res = $query->row_array();
        if(empty($res)) {
            $this->db->insert('hd_holiday_reading_quanity',$info);
            $id = $this->db->insert_id();
        } else {
            $this->db->where('user_id',$info['user_id']);
            $this->db->update('hd_holiday_reading_quanity',$info);
        }
        return true;
    }


    public function updateReadingQuanityRankList() {
        $this->db->from('hd_holiday_reading_quanity');
        $this->db->order_by('quanity','desc');
        $this->db->order_by('reading_quanity_time_create','asc');
        $query = $this->db->get();
        $list = $query->result_array();

        $update_arr = array();
        foreach ($list as $key => $value) {
            $update_arr[] = array(
                'reading_quanity_id' => $value['reading_quanity_id'],
                'rank' => $key+1
            );
        }
        $this->db->update_batch('hd_holiday_reading_quanity',$update_arr,'reading_quanity_id');
    }

}
